package com.hn.gege.sys.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hn.gege.sys.modules.sys.service.IDictService;
import com.hn.gege.sys.modules.sys.entity.Dict;
import com.hn.gege.core.common.CommonPage;
import com.hn.gege.core.common.CommonResult;
import com.hn.gege.core.common.MyException;
import com.hn.gege.core.common.BaseController;
import com.hn.gege.core.query.QueryGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;


@Api(tags = "SysDictController", description = "字典类别表管理")
@RestController
@RequestMapping("/sys/dict")
@Validated
public class DictController extends BaseController {

    @Autowired
    IDictService dictService;

    @ApiOperation("字典类别表分页列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/pageInfo")
    public CommonPage<Dict> pageInfo(@NotNull Integer pageNo,
                                     @NotNull Integer pageSize,
                                     Dict dict,
                                     HttpServletRequest request){
        Page<Dict> pageInfo = new Page<>(pageNo, pageSize);

        //构造查询条件
        QueryWrapper<Dict> qw = QueryGenerator.initQueryWrapper(dict, request.getParameterMap());
        Page<Dict> page1 = dictService.page(pageInfo, qw);
        return CommonPage.restPage(page1);
    }

    @ApiOperation("通过主键获取字典类别表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "dictType", value = "字典类别代码", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/{dictType}")
    public CommonResult<Dict> getByPk(@NotBlank @PathVariable("dictType") String dictType){
        Dict dict = dictService.getByPk(dictType);
        return CommonResult.success(dict);
    }

    @ApiOperation("新增字典类别表")
    @PostMapping
    public CommonResult add(@Validated Dict dict, BindingResult bindingResult) throws MyException {
        String dictType = dict.getDictType().toUpperCase();
        check(dictType);

        dict.setDictType(dictType);
        boolean b = dictService.save(dict);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("修改字典类别表")
    @PutMapping
    public CommonResult updateByPk(@Validated Dict dict, BindingResult bindingResult){
        boolean b = dictService.updateByPk(dict);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("通过主键删除字典类别表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "dictType", value = "字典类别代码", required = true, dataType = "String", paramType = "path")
    })
    @DeleteMapping("/{dictType}")
    public CommonResult removeByPk(@NotBlank @PathVariable("dictType") String dictType){
        boolean b = dictService.removeByPk(dictType);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }


    /**
     * 校验字典类别是否存在
     * @param dictType
     */
    private void check(String dictType) throws MyException {
        Dict byId = dictService.getByPk(dictType);
        if(byId != null){
            throw new MyException("字典类别代码已存在");
        }
    }


}
